//-----------------------------------------------------------------------
// <copyright file="CustomerProductDAO.cs" company="Pyramid Consulting">
//     Copyright (c) Pyramid Consulting. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
	
using System;
using System.Data;
using MIMS.WRS.Entities;
using MyFramework;
using MyFramework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace MIMS.WRS.Data
{	
	public class CustomerProductDAO : CustomerProductDAOBase
	{
		public CustomerProductDAO()
		{
		}

		public virtual CustomerProductCollection GetCustomerProducts(int userID, CustomerProductColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
		{            
			try
			{
				Database database = DatabaseFactory.CreateDatabase();
				DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spCustomerProductsGetListByCustomer");
                
				dbCommandWrapper.AddInParameter("@userID", DbType.Int32, userID);
				dbCommandWrapper.AddInParameter("@OrderBy", DbType.AnsiString, orderBy.ToString());
				dbCommandWrapper.AddInParameter("@OrderDirection", DbType.AnsiString, orderDirection.ToString());
				dbCommandWrapper.AddInParameter("@Page", DbType.Int32, page);
				dbCommandWrapper.AddInParameter("@PageSize", DbType.Int32, pageSize);
				dbCommandWrapper.AddOutParameter("@TotalRecords", DbType.Int32, 4);
                
				CustomerProductCollection customerProductCollection = new CustomerProductCollection();
				using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
				{
					while (reader.Read())
					{
						CustomerProduct customerProduct = CreateCustomerProductFromReader(reader);
						
						if (!reader.IsDBNull(reader.GetOrdinal("LoginName"))) customerProduct.LoginName = (string)reader["LoginName"];
						if (!reader.IsDBNull(reader.GetOrdinal("ConPointDescription"))) customerProduct.ConPointDescription = (string)reader["ConPointDescription"];

						customerProductCollection.Add(customerProduct);
					}
					reader.Close();
				}
				totalRecords = (int)dbCommandWrapper.GetParameterValue("@TotalRecords");
				return customerProductCollection;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException(SR.DataAccessGetCustomerProductsException, ex);
			}
		}

		#region "override methods"	
		#region CreateCustomerProduct methods
            
		public override void CreateCustomerProduct(CustomerProduct customerProduct)
		{
			try
			{
				Database database = DatabaseFactory.CreateDatabase();
				DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spCustomerProductsInsert");
                
				dbCommandWrapper.AddInParameter("@UserID", DbType.Int32, customerProduct.UserID);
				dbCommandWrapper.AddInParameter("@ProductCode", DbType.AnsiString, customerProduct.ProductCode);
				dbCommandWrapper.AddInParameter("@ReservedQty", DbType.Int32, customerProduct.ReservedQty==Nulls.NullInteger?DBNull.Value:(object)customerProduct.ReservedQty);
				dbCommandWrapper.AddInParameter("@ReservedDate", DbType.DateTime, customerProduct.ReservedDate==Nulls.NullDate?DBNull.Value:(object)customerProduct.ReservedDate);
                dbCommandWrapper.AddInParameter("@NumberOfDay", DbType.Int32, customerProduct.NumberOfDay);
				DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException(SR.DataAccessCreateCustomerProductException, ex);
			}
		}

		#endregion

		#region UpdateCustomerProduct methods
        
		public override void UpdateCustomerProduct(CustomerProduct customerProduct)
		{
			try
			{
				Database database = DatabaseFactory.CreateDatabase();
				DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spCustomerProductsUpdate");            
                
				dbCommandWrapper.AddInParameter("@UserID", DbType.Int32, customerProduct.UserID);
				dbCommandWrapper.AddInParameter("@ProductCode", DbType.AnsiString, customerProduct.ProductCode);
				dbCommandWrapper.AddInParameter("@ReservedQty", DbType.Int32, customerProduct.ReservedQty==Nulls.NullInteger?DBNull.Value:(object)customerProduct.ReservedQty);
				dbCommandWrapper.AddInParameter("@ReservedDate", DbType.DateTime, customerProduct.ReservedDate==Nulls.NullDate?DBNull.Value:(object)customerProduct.ReservedDate);
                dbCommandWrapper.AddInParameter("@NumberOfDay", DbType.Int32, customerProduct.NumberOfDay);
				DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException(SR.DataAccessUpdateCustomerProductException, ex);
			}
		}
        
		#endregion
		#endregion
	}
}
